package com.gable.exhibition.controller;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.gable.common.util.FileUploadUtil;
import com.gable.common.util.ReturnResultUtil;
import com.gable.exhibition.service.FileUploadService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.FileView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by Lesson on 2018/2/22.
 */
@Controller
@RequestMapping(value="/file")
public class FileController {
    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    FileUploadService fileUploadService;

    @RequestMapping(value = "/fileDownload")
    public ModelAndView fileDownload(HttpServletRequest request, HttpServletResponse response, @RequestParam("filePath")String filePath, @RequestParam("fileName")String fileName) throws IOException {
        OSSClient ossClient = FileUploadUtil.getOssClient();
        filePath = subFilePath(filePath);
        OSSObject ossObject = ossClient.getObject(FileUploadUtil.BUCKETNAME, filePath);
        if(ossObject != null){
            InputStream in = ossObject.getObjectContent();
            byte[] bytes = FileUploadUtil.input2byte(in);
            in.close();
            ossClient.shutdown();
            response.setContentType("application/x-download");
            response.setHeader("Content-disposition", "attachment;filename="+new String(fileName.getBytes("gbk"), "iso8859-1"));
            OutputStream ouputStream = response.getOutputStream();
            ouputStream.write(bytes);
            ouputStream.flush();
            ouputStream.close();
        }
        return null;
    }

    @RequestMapping(value = "/queryFileList",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryFileList(HttpServletRequest request, FileView fileView){
        ReturnResultUtil resultUtil =  new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
        try {
            Long keyId = fileView.getKeyId();
            Long keyType = fileView.getKeyType();
            if(keyId == null)
                return new ReturnResultUtil(500,MessageUtil.KEYIDISNULL);
            if(keyType == null)
                return new ReturnResultUtil(500,MessageUtil.KEYTYPEISNULL);
            resultUtil = fileUploadService.queryFileList(fileView);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultUtil;
    }

    public String subFilePath(String str){
        String[] array = str.split("/");
        return array[array.length-2]+"/"+array[array.length-1];
    }
}
